package com.wit.coursesystem.dbhelper;

import android.content.Context;

import com.wit.coursesystem.dao.DaoSession;
import com.wit.coursesystem.dao.StudentDao;
import com.wit.coursesystem.entity.Student;

public class StudentHelper {
	private StudentDao studentDao;
	private static Context mContext;
	private static StudentHelper instance;
	
	private StudentHelper() {}
	
	public static StudentHelper getInstance(Context context) {
		if(instance == null) {
			instance = new StudentHelper();
			if(mContext == null)
				mContext = context;
			DaoSession session = DbHelper.getDaoSession(mContext);
			instance.studentDao = session.getStudentDao();
		}
		
		return instance;
	}
	
	public long insert(Student student) {
		return studentDao.insert(student);
	}
	
	public Student selectByKey(long id) {
		return studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(id)).unique();
	}
	
	public Student selectByPwd(Student student) {
		return studentDao.queryBuilder().where(StudentDao.Properties.StudentNum.eq(student.getStudentNum()), 
				StudentDao.Properties.Password.eq(student.getPassword())).unique();
	}
	
	public Student selectByStudentNum(String stuNum) {
		return studentDao.queryBuilder().where(StudentDao.Properties.StudentNum.eq(stuNum)).unique();
	}
}
